/**
 * 控制器
 * Created by KY on 2017/7/25.
 */
define(['require','ldfw/core','ctrl/BaseCtrl','comps/session','page/IndexView','page/MainView',
        'page/AuthenticationView','page/RiskAssessmentView','page/RiskResultView',
        'page/position/DetailView', 'page/dividend/DetailView','page/product/DetailView','page/product/NewsView',
        'page/user/ChangePwdView','page/notification/ListView','page/notification/DetailView',
        'page/user/ProfileView','page/user/QuestionnaireView','page/user/ProfileEditView','page/product/PurchaseView',
        'page/product/AppropConfirmView','page/product/RiskExplainView','page/product/RiskWarnView','page/transform/ListView',
        'page/transform/DetailView','page/transform/AddView','page/position/TimeLineView',
        'page/transaction/ListView','page/news/DetailView','page/product/WarnConfirmView','page/appointaction/ListView','page/product/AppointView'],
function(require,LDFW,BaseCtrl,session){
    return BaseCtrl.extend({
        validateLogin:function(){
            session.setItem('request_url',window.location.hash)
            let user = session.getCurrentUser()
            if(!user){
                LDFW.navigate('login')
                return false
            }
            return true
        },
        index: function (url) {
            let user = session.getCurrentUser(),
                _this = this,
                view = require('page/IndexView');
            if(user) {
                if(!user.cust_no){
                    LDFW.navigate('authentication')
                }else if(!user.ques_invest_num || !user.risk_accept){
                    LDFW.navigate('risk_assessment')
                }else{
                    LDFW.navigate('main')
                }
                return
            }
            if(global_config.show_index===0){
                LDFW.navigate('login')
                return
            }
            var page = {
                url:url,
                view:view
            }
            if(_this._findInStack(url)){
                _this._back(page)
            }else{
                _this._show(page)
            }
        },
        login: function (url){
            var _this = this
            require(['page/LoginView'],function(view){
                var page = {
                    url:url,
                    view:view
                }
                if(_this._findInStack(url)){
                    _this._back(page)
                }else{
                    _this._show(page)
                }
            })
        },
        register:function(url){
            var _this = this
            require(['page/RegisterView'],function(view){
                var page = {
                    url:url,
                    view:view
                }
                if(_this._findInStack(url)){
                    _this._back(page)
                }else{
                    _this._show(page)
                }
            })
        },

        registerQuestion:function(url){
            var _this = this
            require(['page/RegisterQuestionView'],function(view){
                var page = {
                    url:url,
                    view:view
                }
                if(_this._findInStack(url)){
                    _this._back(page)
                }else{
                    _this._show(page)
                }
            })
        },
        changePwd:function(url){
            if(this.validateLogin()){
                var view = require('page/user/ChangePwdView')
                var page = {
                    url: url,
                    view: view
                }
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        main: function (url,index) {
            if(this.validateLogin()) {
                var view = require('page/MainView')
                var page = {
                    url: url,
                    view: view
                }
                var defaultIndex = 1
                if(index!==null){
                    defaultIndex = index
                }else{
                    if(session.getItem('main-tab-index')!== null){
                        defaultIndex = session.getItem('main-tab-index')
                    }else{
                        defaultIndex = global_config.default_index
                    }
                }
                view.off('change:tabindex').on('change:tabindex', function (index) {
                    session.setItem('main-tab-index', index)
                })
                view.setDefaultIndex(defaultIndex)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        positionDetail: function (url,pd_no) {
            if(this.validateLogin()) {
                var view = require('page/position/DetailView')
                var page = {
                    url: url,
                    view: view
                }
                view.setProductNo(pd_no)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        positionTimeLine:function(url,contra_no){
            if(this.validateLogin()) {
                var view = require('page/position/TimeLineView')
                var page = {
                    url: url,
                    view: view
                }
                view.setContractNo(contra_no)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },

        dividendDetail: function (url,pd_no) {
            if(this.validateLogin()) {
                var view = require('page/dividend/DetailView')
                var page = {
                    url: url,
                    view: view
                }
                view.setProductNo(pd_no)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        productDetail: function (url,pd_no) {
            if(this.validateLogin()) {
                let user = session.getCurrentUser()
                if (user.qual_auth != 1) {
                    $.alert('对不起，您尚未通过审核，无法查看该产品的详细信息')
                    window.history.go(-1)
                    return
                }
                var view = require('page/product/DetailView')
                var page = {
                    url: url,
                    view: view
                }
                view.setProductNo(pd_no)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },

        productNewsDetail:function(url,articleNo){
            if(this.validateLogin()) {
                var view = require('page/product/NewsView')
                var page = {
                    url: url,
                    view: view
                }
                view.setArticleNo(articleNo)
                view.loadData()
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },

        notification:function(url){
            if(this.validateLogin()) {
                var view = require('page/notification/ListView')
                var page = {
                    url: url,
                    view: view
                }
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        notificationDetail:function(url,notice_no){
            if(this.validateLogin()) {
                var view = require('page/notification/DetailView')
                var page = {
                    url: url,
                    view: view
                }
                view.setNoticeNo(notice_no)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        profile:function(url){
            if(this.validateLogin()) {
                var view = require('page/user/ProfileView')
                var page = {
                    url: url,
                    view: view
                }
                view.loadData()
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        authentication:function(url){
            if(this.validateLogin()) {
                let user = session.getCurrentUser()
                if (!user.cust_no || user.cust_no == '0') {
                    var view = require('page/AuthenticationView')
                    var page = {
                        url: url,
                        view: view
                    }
                    if (this._findInStack(url)) {
                        this._back(page)
                    } else {
                        this._show(page)
                    }
                } else if (!cust.risk_accept || !cust.ques_invest_num) {
                    LDFW.navigate('risk_accept')
                } else {
                    LDFW.navigate('main')
                }
            }
        },
        riskAssessment:function(url){
            if(this.validateLogin()) {
                var view = require('page/RiskAssessmentView')
                var page = {
                    url: url,
                    view: view
                }
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        riskResult:function(url){
            if(this.validateLogin()) {

                var view = require('page/RiskResultView')
                var page = {
                    url: url,
                    view: view
                }
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        questionnaire:function(url){
            if(this.validateLogin()) {

                var view = require('page/user/QuestionnaireView')
                var page = {
                    url: url,
                    view: view
                }
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        profileEdit:function(url,attribute){
            if(this.validateLogin()) {

                var view = require('page/user/ProfileEditView')
                var page = {
                    url: url,
                    view: view
                }
                view.setTitle(attribute)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        purchase:function(url,pd_no){
            if(this.validateLogin()) {

                var view = require('page/product/PurchaseView')
                var page = {
                    url: url,
                    view: view
                }
                view.setProductNo(pd_no)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        appropConfirm:function(url,pd_no){
            if(this.validateLogin()) {

                var view = require('page/product/AppropConfirmView')
                var page = {
                    url: url,
                    view: view
                }
                view.setProductNo(pd_no)
                view.loadData()
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        riskExplain:function(url,pd_no){
            if(this.validateLogin()) {
                var view = require('page/product/RiskExplainView')
                var page = {
                    url: url,
                    view: view
                }
                view.setProductNo(pd_no)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        riskWarn:function(url,pd_no){
            if(this.validateLogin()) {
                var view = require('page/product/RiskWarnView')
                var page = {
                    url: url,
                    view: view
                }
                view.setProductNo(pd_no)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        transform:function(url){
            if(this.validateLogin()) {

                var view = require('page/transform/ListView')
                var page = {
                    url: url,
                    view: view
                }
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        transformDetail:function(url,audit_no){
            if(this.validateLogin()) {

                var view = require('page/transform/DetailView')
                var page = {
                    url: url,
                    view: view
                }
                view.setAuditNo(audit_no)
                view.loadData()
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        transformAdd:function(url){
            if(this.validateLogin()) {
                var view = require('page/transform/AddView')
                var page = {
                    url: url,
                    view: view
                }
                view.loadData()
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        transaction:function(url){
            if(this.validateLogin()) {
                var view = require('page/transaction/ListView')
                var page = {
                    url: url,
                    view: view
                }
                view.loadData()
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        newsDetail:function(url,articleNo){
            if(this.validateLogin()) {
                var view = require('page/news/DetailView')
                var page = {
                    url: url,
                    view: view
                }
                view.setArticleNo(articleNo)
                view.loadData()
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        warnConfirm:function(url,pd_no){
            if(this.validateLogin()) {
                var view = require('page/product/WarnConfirmView')
                var page = {
                    url: url,
                    view: view
                }
                view.setPdNo(pd_no)
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        appointaction:function(url){
            if(this.validateLogin()) {
                var view = require('page/appointaction/ListView')
                var page = {
                    url: url,
                    view: view
                }
                view.loadData()
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        appoint:function(url,pd_no){
            if(this.validateLogin()) {

                var view = require('page/product/AppointView')
                var page = {
                    url: url,
                    view: view
                }
                view.setProductNo(pd_no)
                view.loadData()
                if (this._findInStack(url)) {
                    this._back(page)
                } else {
                    this._show(page)
                }
            }
        },
        detail(url,pd_no){

        }
    })
})